<template>
  <div id="products" class="card col-12 px-0 mt-5">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item ms-1" aria-current="page">系统管理</li>
        <li class="breadcrumb-item active" aria-current="page">服务产品</li>
      </ol>
    </nav>
    <template v-if="isSearchMode">
      <div class="card-body">
        <form>
          <div class="row mb-2">
            <label>状态</label>
            <select class="form-select" v-model.number="status">
              <option value="-1">全部</option>
              <option value="0">停用</option>
              <option value="1">启用</option>
            </select>
          </div>

          <div class="row mb-2">
            <select
              class="form-select form-control-sm"
              v-model.number="sc.pageSize"
            >
              <option value="5">5</option>
              <option value="10">10</option>
              <option value="20">20</option>
              <option value="50">50</option>
            </select>
          </div>
          <div class="row mb-2">
            <button
              type="button"
              class="btn btn-primary"
              v-on:click="search()"
            >
              查找
            </button>
          </div>
        </form>
      </div>
    </template>
    <template v-else>
      <div class="card-body">
        <button
          type="button"
          @click.stop="newProduct()"
          class="btn btn-success"
          id="btnNewUser"
        >
          新建
        </button>
        <button
          type="button"
          class="btn btn-primary float-end"
          @click.stop="showSearch()"
        >
          查找
        </button>
      </div>

      <table class="table table-hover table-sm">
        <thead>
          <tr>
            <th>名称</th>
            <th>代码</th>
            <th>说明</th>
            <th class="d-none d-md-table-cell">原价</th>
            <th class="d-none d-md-table-cell">优惠价</th>
            <th class="d-none d-md-table-cell">成本</th>
            <th class="d-none d-md-table-cell">状态</th>
            <th class="d-none d-md-table-cell">生成时间</th>
            <th class="d-none d-md-table-cell">修改时间</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <template v-for="(info, index) in dataList">
            <tr @click.stop="showDetail(info.id)">
              <td>{{ info.productName }}</td>
              <td>
                {{ info.productCode }}
              </td>
              <td>
                {{ info.productDesc }}
              </td>
              <td class="d-none d-md-table-cell">
                {{ info.price }}
              </td>
              <td class="d-none d-md-table-cell">
                {{ info.discount }}
              </td>
              <td class="d-none d-md-table-cell">
                {{ info.cost }}
              </td>
              <td class="d-none d-md-table-cell">
                <span class="text-success" v-if="info.status === 1">正常</span>
                <span class="text-danger" v-else>停用</span>
              </td>
              <td class="d-none d-md-table-cell">
                {{ info.createTime }}
              </td>
              <td class="d-none d-md-table-cell">
                {{ info.lastUpdate }}
              </td>
              <td>
                <i class="mdi mdi-arrow-right fa-2"></i>
              </td>
            </tr>
          </template>
        </tbody>
      </table>

      <nav id="pagination-box" class="float-end">
        <my-pagination
          name="pagination"
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </nav>
    </template>
  </div>
</template>

<script>
import { searchProducts } from "@/api/product.js";
import MyPagination from "@/components/my-pagination_h5.vue";

export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      isSearchMode: false,
      dataList: [],
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 10,
        pageTotal: 0,
      },
      status: -1,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showSearch: function () {
      this.isSearchMode = true;
    },
    search: function () {
      this.isSearchMode = false;
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.status": this.status,
      };

      searchProducts(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
        },
        null,
        () => {}
      );
    },
    showDetail: function (id) {
      this.$router.push("/h5/product/" + id);
    },
    newProduct: function () {
      this.$router.push("/h5/product");
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
